package cn.tedu.egweibo04.demos.web.controller;

import cn.tedu.egweibo04.demos.web.comment.response.JsonResult;
import cn.tedu.egweibo04.demos.web.comment.response.StatusCode;
import cn.tedu.egweibo04.demos.web.mapper.UserMapper;
import cn.tedu.egweibo04.demos.web.pojo.dto.UserRegDTO;
import cn.tedu.egweibo04.demos.web.pojo.entity.User;
import cn.tedu.egweibo04.demos.web.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@Api(tags = "01.用户模块")
@RestController
@RequestMapping("/v1/users")
public class UserController {
    @Autowired
    public UserMapper userMapper;
    @PostMapping("reg")
    @ApiOperation(value = "用户注册功能")
    public JsonResult reg(@RequestBody UserRegDTO userRegDTO){
        log.debug("userRegDTO = " + userRegDTO);
        String username = userRegDTO.getUsername();
        UserVO userVO = userMapper.selectByUsername(username);
        log.debug("userVO = " + userVO);
        if(userVO != null){
            return new JsonResult(StatusCode.OPERATION_FAILED,StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok();
    }
}
